package com.xtourms.client.rm.hotel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.client.ui.util.MoneyTextBox;
import com.xtourms.client.util.MysqlDateFormat;
import com.xtourms.client.util.Util;
import com.xtourms.shared.field.HotelField;

public class RoomDetailManager extends Composite {

	private static RoomDetailManagerUiBinder uiBinder = GWT
			.create(RoomDetailManagerUiBinder.class);

	interface RoomDetailManagerUiBinder extends
			UiBinder<Widget, RoomDetailManager> {
	}
	
	private JSONObject roomObj;
	private JSONObject hotelObj;
	private ContentFrame parent;
	private AsyncCallback<String> callback;
	
	@UiField Label hotelName;
	@UiField ListBox roomStyle;
	@UiField ListBox roomBreakfast;
	@UiField MoneyTextBox basePrice;
	@UiField MoneyTextBox actualPrice;
	@UiField MoneyTextBox sellPrice;
	@UiField TextArea roomDes;
	@UiField DateBox startDate;
	@UiField DateBox endDate;
	
	public RoomDetailManager(JSONObject hotel, ContentFrame parent, AsyncCallback<String> callback) {
		initWidget(uiBinder.createAndBindUi(this));
		this.hotelObj = hotel;
		this.roomObj = new JSONObject();
		this.parent = parent;
		this.callback = callback;
		init();
	}
	
	public RoomDetailManager(JSONObject hotel, JSONObject roomObj, ContentFrame parent, AsyncCallback<String> callback){
		initWidget(uiBinder.createAndBindUi(this));
		this.hotelObj = hotel;
		this.roomObj = roomObj;
		this.parent = parent;
		this.callback = callback;
		init();
		initDetail();
	}
	
	private void initDetail() {
		String style = ((JSONString)roomObj.get(HotelField.ROOM_STYLE)).stringValue();
		int index = Util.findIndex(HotelContant.ROOM_STYLE_STRS, style);
		roomStyle.setSelectedIndex(index);
		String breakfast = ((JSONString)roomObj.get(HotelField.ROOM_BREAKFAST)).stringValue();
		index = Util.findIndex(HotelContant.BREAKFAST_STRS, breakfast);
		roomBreakfast.setSelectedIndex(index);
		basePrice.setMoney(((JSONNumber)roomObj.get(HotelField.ROOM_BASE_PRICE)).toString());
		actualPrice.setMoney(((JSONNumber)roomObj.get(HotelField.ROOM_ACTUAL_PRICE)).toString());
		sellPrice.setMoney(((JSONNumber)roomObj.get(HotelField.ROOM_SELL_PRICE)).toString());
		roomDes.setValue(((JSONString)roomObj.get(HotelField.ROOM_DES)).stringValue());
		startDate.setValue(java.sql.Date.valueOf(((JSONString)roomObj.get(HotelField.ROOM_START_DATE)).stringValue()));
		endDate.setValue(java.sql.Date.valueOf(((JSONString)roomObj.get(HotelField.ROOM_END_DATE)).stringValue()));
	}

	private void init(){
		hotelName.setText(((JSONString)hotelObj.get(HotelField.HOTEL_NAME)).stringValue());
		Util.fillListBox(roomStyle, HotelContant.ROOM_STYLE_STRS);
		Util.fillListBox(roomBreakfast, HotelContant.BREAKFAST_STRS);
		startDate.setFormat(new MysqlDateFormat());
		endDate.setFormat(new MysqlDateFormat());
	}

	@UiHandler("back")
	void back(ClickEvent evt){
		parent.resumeContent();
	}
	
	private boolean isValid(){
		if(null == startDate.getValue() || null == endDate.getValue()
				|| -1 == basePrice.getMoney() || -1 == sellPrice.getMoney()
				|| -1 == actualPrice.getMoney()){
			MessageBox.ins.showWarning("请填写必填项。");
			return false;
		}
		if(!(basePrice.isValid() && sellPrice.isValid() && actualPrice.isValid())){
			MessageBox.ins.showWarning("请检查输入金额是否符合规范。");
			return false;
		}
		//FIXME:必须高于费率才有利润
		if(sellPrice.getMoney()*0.97 <= basePrice.getMoney()){
			MessageBox.ins.showWarning("请检查销售价格是否过低。");
			return false;
		}
		if(sellPrice.getMoney() > actualPrice.getMaxLength()){
			MessageBox.ins.showWarning("请确认销售价格不应该高于挂牌价。");
			return false;
		}
		if(startDate.getValue().after(endDate.getValue())){
			MessageBox.ins.showWarning("终止日期不能早于起始日期。");
			return false;
		}
		return true;
	}
	
	@UiHandler("confirm")
	void confirm(ClickEvent evt){
		if(!isValid()){
			return;
		}
		
		roomObj.put(HotelField.ROOM_STYLE, new JSONString(roomStyle.getValue(roomStyle.getSelectedIndex())));
		roomObj.put(HotelField.ROOM_BREAKFAST, new JSONString(roomBreakfast.getValue(roomBreakfast.getSelectedIndex())));
		roomObj.put(HotelField.ROOM_START_DATE, new JSONString(startDate.getValue().toString()));
		roomObj.put(HotelField.ROOM_END_DATE, new JSONString(endDate.getValue().toString()));
		roomObj.put(HotelField.ROOM_DES, new JSONString(roomDes.getValue()));
		roomObj.put(HotelField.ROOM_BASE_PRICE, new JSONNumber(basePrice.getMoney()));
		roomObj.put(HotelField.ROOM_ACTUAL_PRICE, new JSONNumber(actualPrice.getMoney()));
		roomObj.put(HotelField.ROOM_SELL_PRICE, new JSONNumber(sellPrice.getMoney()));
		roomObj.put(HotelField.HOTEL_ID, hotelObj.get(HotelField.HOTEL_ID));

		HotelManagerFrame.hmsa.cOrMRoom(roomObj.toString(), callback);
	}
}
